package org.lightweightimageviewer.liv;

import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;


/**
 * Home for User's settings.
 * @author Steven Boscarine
 *
 */
public class UserPreferences {
	public static enum PropertyKeys{
		scale("Scale Factor for large images"), 
		next("Keys to load next image"), 
		previous("Keys to load previous image"), 		
		zoomIn("Keys to zoom in"), 		
		zoomOut("Keys to zoom out"), 		
		slideshow("Keys to start or stop slideshow"), 
		slideshowIncrement("Number of milliseconds between image loads in slideshow."),
		fetchForward("Number of forward images to cache into memory"), 
		fetchBackward("Number of previous images to cache into memory");
		PropertyKeys(String explanation){
			this.explanation=explanation;
		}
		final String explanation;
		public String getExplanation() {
			return explanation;
		}
	}
	/**
	 * Delimiter for storing keymappings.
	 */
	private static final String DELIMITER = ",";
	/**
	 * name/location of properties file storing values.
	 */
	private static final String PROPERTIES_FILE = "/liv.properties";
	public final static Properties properties = new Properties();

	
	public static final Set<Integer> nextImage = new HashSet<Integer>();
	public static final Set<Integer> previousImage = new HashSet<Integer>();
	public static final Set<Integer> slideshow = new HashSet<Integer>();
	public static final Set<Integer> zoomIn = new HashSet<Integer>();
	public static final Set<Integer> zoomOut = new HashSet<Integer>();
	
	public static long SLIDESHOW_INCREMENT;
	public static double SCALE;
	public static int FETCH_NEXT;
	public static int FETCH_PREVIOUS;
	static final int NUM_THREADS=10;
	static{
		init(PROPERTIES_FILE);
	}
	//may be invoked by unit tests once validation is added.  
	static final void init(String propertiesFileName){
		try {
			properties.load(UserPreferences.class.getResourceAsStream (PROPERTIES_FILE));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		SCALE = Double.parseDouble(properties.getProperty(PropertyKeys.scale.name()));
		
		final String tmp0 = properties.getProperty(PropertyKeys.next.name());
		final String tmp1 = properties.getProperty(PropertyKeys.previous.name());
		final String tmp2 = properties.getProperty(PropertyKeys.slideshow.name());
		final String tmp3 = properties.getProperty(PropertyKeys.zoomIn.name());
		final String tmp4 = properties.getProperty(PropertyKeys.zoomOut.name());
		SLIDESHOW_INCREMENT = Long.parseLong(properties.getProperty(PropertyKeys.slideshowIncrement.name()));
		FETCH_NEXT = Integer.parseInt(properties.getProperty(PropertyKeys.fetchForward.name()));
		FETCH_PREVIOUS = Integer.parseInt(properties.getProperty(PropertyKeys.fetchForward.name()));
		for(String token : tmp0.split(DELIMITER)){
			nextImage.add(Integer.parseInt(token));
		}
		for(String token : tmp1.split(DELIMITER)){
			previousImage.add(Integer.parseInt(token));
		}
		for(String token : tmp2.split(DELIMITER)){
			slideshow.add(Integer.parseInt(token));
		}
		for(String token : tmp3.split(DELIMITER)){
			zoomIn.add(Integer.parseInt(token));
		}
		for(String token : tmp4.split(DELIMITER)){
			zoomOut.add(Integer.parseInt(token));
		}
	}
}